﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.product
{
    public partial class shoppingcart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Form["Option"] != null && !Request.Form["Option"].Trim().Equals(""))
            {

                string types = Request.Form["Option"].Trim();
                if (types == "SubmitOrders")
                {
                    //PrintTotal();
                }
                else if (types == "del")
                {
                    string id = Request.Form["id"].Trim();
                    this.DelShoppCart(id);
                }
                else if (types == "MofiyInfo")
                {
                    string id = Request.Form["id"].Trim();
                    string productcount = Request.Form["productcount"].Trim();
                    YXShop.BLL.Product.ProductInfo infoBll = new YXShop.BLL.Product.ProductInfo();
                    if(id!="" && ChangeHope.Common.ValidateHelper.IsNumber(id))
                    {
                        string productId = ChangeHope.WebPage.PageRequest.GetFormString("productId");
                        
                        YXShop.Model.Product.ProductInfo model = infoBll.GetModel(Convert.ToInt32(productId));
                        if(model!=null)
                        {
                            if (Convert.ToInt32(model.pro_Stock) < Convert.ToInt32(productcount))
                            {
                                Response.Write("ok");
                            }
                            else
                            {
                                this.ModfiyProductCount(id, productcount);
                            }
                        }
                    }             
                }
                Response.End();
                return;
            }
            if (!IsPostBack)
            {
                Response.Write(YXShop.TemplateAction.Common.YXShop_PageContent("ShoppingCart"));
            }
        }

        private void ModfiyProductCount(string id, string productcount)
        {
            ProfileCommon Profile = new ProfileCommon();
            int Count = int.Parse(productcount);
            if (Count > 0)
            {
                Profile.ShoppingCart.SetQuantity(id, int.Parse(productcount));
            }
            else if (Count == 0)
            {
                Profile.ShoppingCart.Remove(id);
            }
            HttpContext.Current.Profile.Save();
        }

        private void DelShoppCart(string id)
        {
            ProfileCommon Profile = new ProfileCommon();
            Profile.ShoppingCart.Remove(id.ToString());
            HttpContext.Current.Profile.Save();
        }
    }
}
